#include <Timer.h>
#include <UserButton.h>
#include "MainAppC.h"
#include "Package.h"

module MainAppC {
	uses interface Boot;
	uses interface Notify<button_state_t> as Button;
	uses interface LEDControl;
	uses interface SendControl;
	uses interface Data;
	uses interface StateInterface;
}
implementation {
	uint8_t mode = Passive;

	event void Boot.booted() {
		call Button.enable();
		call StateInterface.InitStates();
	}

	event void Button.notify(button_state_t state) {
		if(state == BUTTON_PRESSED) {
			switch(mode) {
				case Passive : mode = Receive;
				break;

				case Receive : mode = Send;
				break;

				case Send : mode = Passive;
				break;

				default : mode = Passive;
				break;
			}

			switch(mode) {
				case Passive : call LEDControl.On(LEDState_ModePassive);
				call StateInterface.ChangeState(Passive);
				break;

				case Receive : call LEDControl.On(LEDState_ModeReceive);
				call StateInterface.ChangeState(Receive);
				break;

				case Send : call LEDControl.On(LEDState_ModeSend);
				call StateInterface.ChangeState(Send);
				break;

				default : call LEDControl.On(LEDState_ModePassive);
				break;
			}
		}
		else 
			if(state == BUTTON_RELEASED) {
			call LEDControl.Off(LEDState_AllOff);
		}
	}

}